package com.weigecoder.common.result;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResponseUtil {
    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string 待渲染的字符串
     * @return null
     */
    public static void out(HttpServletResponse response, Result string) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpStatus.OK.value());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        //response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        try {
            mapper.writeValue(response.getWriter(), string);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
